package Common;

public class Utilities
{

    public static String IntToBinary(int i, int width)
    {
        try
        {
            String str = Integer.toBinaryString(i);
            while (str.length() < width)
            {
                str = "0" + str;
            }
            return str;
        }
        catch (NumberFormatException ex)
        {

        }
        return "";
    }

    public static String IntTo4Hex(int i)
    {
        if (i >= 0)
        {
            return String.format("%04X", i);
        }
        else
        {
            String str = String.format("%04X", i);
            return str.substring(str.length()-4, str.length());
        }
    }

    public static int ParseInteger(String s)
    {
        if (s.startsWith("+"))
        {
            return Integer.parseInt(s.substring(1));
        }
        else
        {
            return Integer.parseInt(s);
        }
    }


    public static boolean ValidateLabel(String label)
    {
        if (label == null) return true;
        return label.matches("[a-zA-Z][\\w]{0,31}");
    }

    public static boolean ValidateLiteral(String literal)
    {
        boolean retBool = false;
        char firstChar = Character.toUpperCase(literal.charAt(0));
        //System.out.print("Validating " + literal + " | ");
        if (firstChar == 'I')
        {
            if (literal.charAt(1) == '=')
            {
                if(literal.charAt(2) == '+')
                {
                    retBool = ValidateIntRange(literal.substring(3, literal.length()),-32768,32767);
                }
                else
                {
                    retBool = ValidateIntRange(literal.substring(2, literal.length()),-32768,32767);
                }
            }

        }
        else if (firstChar == 'C')
        {
            if (literal.matches("[Cc]='.+'"))
            {
                if (literal.length() <= 6)
                {
                    retBool = true;
                }
            }
        }
        else if (firstChar == 'B')
        {
            if (literal.charAt(1) == '=')
            {
                retBool = ValidateBinaryRange(literal.substring(2,literal.length()),0,65535);
            }
        }
        else if (firstChar == 'X')
        {
            if (literal.charAt(1) == '=')
            {
                retBool = ValidateHexRange(literal.substring(2,literal.length()),0,65535);
            }
       
        }
        //System.out.println(" returning " + retBool);
        return retBool;
    }

    public static int EvaluateLiteral(String literal)
    {
        char firstChar = Character.toUpperCase(literal.charAt(0));
        //System.out.print("Validating " + literal + " | ");
        if (firstChar == 'I')
        {
            if (literal.charAt(1) == '=')
            {
                if(literal.charAt(2) == '+')
                {
                    return Integer.valueOf(literal.substring(3, literal.length()));
                }
                else
                {
                    return Integer.valueOf(literal.substring(2, literal.length()));
                }
            }

        }
        else if (firstChar == 'C')
        {
            if (literal.matches("[Cc]='.+'"))
            {
                if (literal.length() == 5)
                {
                    return literal.charAt(3);
                }
                else if (literal.length() == 6)
                {
                    return literal.charAt(3)*0x100 + literal.charAt(4);
                }
            }
        }
        else if (firstChar == 'B')
        {
            if (literal.charAt(1) == '=')
            {
                return Integer.valueOf(literal.substring(2,literal.length()), 2);
            }
        }
        else if (firstChar == 'X')
        {
            if (literal.charAt(1) == '=')
            {
                return Integer.valueOf(literal.substring(2,literal.length()), 16);
            }

        }
        return 0;
    }

    public static boolean ValidateIntRange(String value, int min, int max)
    {
        //System.out.println("Validating int Range for " + value + " between " + min + " and " + max);

        try
        {
            int val = Integer.valueOf(value);
            if(val >=min && val <= max)
            {
                //System.out.println("Range is valid");
                return true;
            }
        }
        catch(NumberFormatException ex)
        {
            //ex.printStackTrace();
            //number would not format
        }
        return false;
    }

    public static boolean ValidateHexRange(String value, int min, int max)
    {
        //System.out.println("Validating int Range for " + value + " between " + min + " and " + max);

        try
        {
            int val = Integer.valueOf(value, 16);
            if(val >=min && val <= max)
            {
                return true;
            }
        }
        catch(NumberFormatException ex)
        {
            //ex.printStackTrace();
            //number would not format
        }
        return false;
    }

    public static boolean ValidateBinaryRange(String value, int min, int max)
    {
        //System.out.println("Validating int Range for " + value + " between " + min + " and " + max);

        try
        {
            int val = Integer.valueOf(value, 2);
            if(val >=min && val <= max)
            {
                return true;
            }
        }
        catch(NumberFormatException ex)
        {
            //ex.printStackTrace();
            //number would not format
        }
        return false;
    }
   
}
